﻿using System;
using System.Collections.Generic;
using System.Linq;
using SimplexEngine;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace OhMyGod
{
    public class OhMyGod : Core
    {
        protected FPSCounter fps_counter = new FPSCounter();
        TextSprite fps_text = null;
        float angle = 0.0f;

        public OhMyGod()
        {
        }

        protected override void Update(GameTime game_time)
        {
            fps_text.Text = "FPS: " + fps_counter.FPS.ToString();
            angle = (float)game_time.TotalGameTime.TotalSeconds / 20.0f;
            float distance = 5.0f;

            left_node.Rotation = Quaternion.CreateFromYawPitchRoll((float)game_time.TotalGameTime.TotalSeconds, 0.0f, 0.0f);
            right_node.Rotation = Quaternion.CreateFromYawPitchRoll((float)game_time.TotalGameTime.TotalSeconds, 0.0f, 0.0f);            

            for (int i = 0; i < point_light_nodes.Count; ++i)
                point_light_nodes[i].Translation = new Vector3(distance * (i + 5) * (float)Math.Sin(angle * (i + 1)), 60.0f + distance * i * (float)(Math.Cos(angle * (point_light_nodes.Count - i) + 1.0f) / 2.0f), -10.0f);
            base.Update(game_time);
        }

        protected override void Draw(GameTime game_time)
        {
            fps_counter.Update();
            base.Draw(game_time);
        }

        SceneNode left_node = null;
        SceneNode right_node = null;
        SceneNode center_node = null;

        protected DirectionalLight direction_light = null;

        List<SceneNode> point_light_nodes = new List<SceneNode>();

        protected override void LoadContent()
        {
            base.LoadContent();          
            SceneManager scene_manager = Root.Instance.CreateSceneManager("Main", "Normal");
            left_node = scene_manager.RootNode.CreateChildNode("LeftNode");
            left_node.Translation = new Vector3(70.0f, 0.0f, 0.0f);
            right_node = scene_manager.RootNode.CreateChildNode("RightNode");
            right_node.Translation = new Vector3(-70.0f, 0.0f, 0.0f);
            center_node = scene_manager.RootNode.CreateChildNode("CenterNode");
            center_node.Translation = new Vector3(0.0f, 60.0f, 0.0f);

            // camera
            SceneNode camera_node = scene_manager.RootNode.CreateChildNode("CameraNode");
            camera_node.Translation = new Vector3(0.0f, 60.0f, -200.0f);
            camera_node.Rotation = Quaternion.CreateFromYawPitchRoll(MathHelper.Pi, 0.0f, 0.0f);
            Camera camera = scene_manager.CreateCamera("MainCamera", SceneObjectFilter.ENTITY, SceneObjectFilter.LIGHT);
            camera.FarPlane = 1000.0f;            
            camera_node.AddSceneObject(camera);
            Root.Instance.RenderSystem.DefaultMRTContext.ViewArea.Camera = camera;
            Root.Instance.RenderSystem.DefaultLightingContext.ViewArea.Camera = camera;

            AnimationMeshEntity md2_model = scene_manager.CreateAnimationMeshEntity("MD2", "Tris.md2");
            md2_model.ActiveAnimation = "point";
            md2_model.LoopMode = AnimationLoopMode.LOOP;
            center_node.AddSceneObject(md2_model);            

            AnimationMeshEntity left_md5_model = scene_manager.CreateAnimationMeshEntity("LeftMD5", "HellKnight.MD5");
            left_node.AddSceneObject(left_md5_model);
            left_md5_model.SetMaterialToAllPart(new MRTBasicMaterial(), false, true);

            AnimationMeshEntity right_md5_model = scene_manager.CreateAnimationMeshEntity("RightMD5", "HellKnight.MD5");
            right_node.AddSceneObject(right_md5_model);            

            // direction light
            
            SceneNode light_node = scene_manager.RootNode.CreateChildNode("LightNode");
            direction_light = scene_manager.CreateLight("DirectionLight", "Direction") as DirectionalLight;
            direction_light.Diffuse = new Color(0.5f, 0.5f, 0.5f);
            light_node.AddSceneObject(direction_light);
            direction_light.Direction = Vector3.Normalize(new Vector3(1.0f, -1.0f, 1.0f));
            

            Random random = new Random();

            for (int i = 0; i < 20; ++i)
            {
                SceneNode point_light_node = scene_manager.RootNode.CreateChildNode("PointLightNode" + i);
                PointLight point_light = scene_manager.CreateLight("PointLight" + i, "Point") as PointLight;
                point_light_node.AddSceneObject(point_light);
                point_light.Range = 20.0f;
                point_light.Diffuse = new Color((float)random.NextDouble(), (float)random.NextDouble(), (float)random.NextDouble());
                point_light_nodes.Add(point_light_node);
            }

            fps_text = new TextSprite("DebugFont.Font", "Test", new Vector2(0.0f), new Vector2(0.0f), 0.0f, new Vector2(1.0f), Color.White);

            Root.Instance.RenderSystem.SpriteManager.AddSprite(fps_text);
            //Root.Instance.RenderSystem.Ambient = Color.White;
            Root.Instance.RenderSystem.Ambient = Color.Black;
            //Root.Instance.RenderSystem.Ambient = new Color(0.3f, 0.3f, 0.3f);
        }
    }
}
